// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.iso.areas.SafeHouse;
import zombie.network.GameClient;
import zombie.network.PacketValidator;
import zombie.network.ServerOptions;

public class SyncSafehousePacket implements INetworkPacket {
    final byte requiredManagerAccessLevel = 56;
    int x;
    int y;
    short w;
    short h;
    public String ownerUsername;
    ArrayList<String> members = new ArrayList();
    ArrayList<String> membersRespawn = new ArrayList();
    public boolean remove = false;
    String title = "";
    public SafeHouse safehouse;
    public boolean shouldCreateChat;

    public void set(SafeHouse _safehouse, boolean doRemove) {
        this.x = _safehouse.getX();
        this.y = _safehouse.getY();
        this.w = (short)_safehouse.getW();
        this.h = (short)_safehouse.getH();
        this.ownerUsername = _safehouse.getOwner();
        this.members.clear();
        this.members.addAll(_safehouse.getPlayers());
        this.membersRespawn.clear();
        this.membersRespawn.addAll(_safehouse.playersRespawn);
        this.remove = doRemove;
        this.title = _safehouse.getTitle();
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.x = b.getInt();
        this.y = b.getInt();
        this.w = b.getShort();
        this.h = b.getShort();
        this.ownerUsername = zombie.GameWindow.ReadString(b);
        short short0 = b.getShort();
        this.members.clear();

        for (int int0 = 0; int0 < short0; int0++) {
            this.members.add(zombie.GameWindow.ReadString(b));
        }

        short short1 = b.getShort();

        for (int int1 = 0; int1 < short1; int1++) {
            this.membersRespawn.add(zombie.GameWindow.ReadString(b));
        }

        this.remove = b.get() == 1;
        this.title = zombie.GameWindow.ReadString(b);
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putInt(this.x);
        b.putInt(this.y);
        b.putShort(this.w);
        b.putShort(this.h);
        b.putUTF(this.ownerUsername);
        b.putShort((short)this.members.size());

        for (String string0 : this.members) {
            b.putUTF(string0);
        }

        b.putShort((short)this.membersRespawn.size());

        for (String string1 : this.membersRespawn) {
            b.putUTF(string1);
        }

        b.putByte((byte)(this.remove ? 1 : 0));
        b.putUTF(this.title);
    }

    public void process() {
        this.safehouse = SafeHouse.getSafeHouse(this.x, this.y, this.w, this.h);
        this.shouldCreateChat = false;
        if (this.safehouse == null) {
            this.safehouse = SafeHouse.addSafeHouse(this.x, this.y, this.w, this.h, this.ownerUsername, GameClient.bClient);
            this.shouldCreateChat = true;
        }

        if (this.safehouse != null) {
            this.safehouse.getPlayers().clear();
            this.safehouse.getPlayers().addAll(this.members);
            this.safehouse.playersRespawn.clear();
            this.safehouse.playersRespawn.addAll(this.membersRespawn);
            this.safehouse.setTitle(this.title);
            this.safehouse.setOwner(this.ownerUsername);
            if (this.remove) {
                SafeHouse.getSafehouseList().remove(this.safehouse);
                DebugLog.log("safehouse: removed " + this.x + "," + this.y + "," + this.w + "," + this.h + " owner=" + this.safehouse.getOwner());
            }
        }
    }

    public boolean validate(UdpConnection connection) {
        boolean _boolean = (connection.accessLevel & 56) != 0;
        this.safehouse = SafeHouse.getSafeHouse(this.x, this.y, this.w, this.h);
        if (this.safehouse == null) {
            if (connection.accessLevel == 1 && SafeHouse.hasSafehouse(this.ownerUsername) != null) {
                if (ServerOptions.instance.AntiCheatProtectionType19.getValue() && PacketValidator.checkUser(connection)) {
                    PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type19", this.getDescription());
                }

                return false;
            } else {
                double _double = 100.0 * ServerOptions.instance.AntiCheatProtectionType20ThresholdMultiplier.getValue();
                if (connection.accessLevel == 1 && ((double)this.h > _double || (double)this.w > _double)) {
                    if (ServerOptions.instance.AntiCheatProtectionType20.getValue() && PacketValidator.checkUser(connection)) {
                        PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type20", this.getDescription());
                    }

                    return false;
                } else {
                    return true;
                }
            }
        } else {
            return !_boolean ? true : PacketValidator.checkSafehouseAuth(connection, this.safehouse.getOwner(), this.getClass().getSimpleName());
        }
    }

    @Override
    public String getDescription() {
        String string = "\n\t" + this.getClass().getSimpleName() + " [";
        string = string + "position=(" + this.x + ", " + this.y + ", " + this.w + ", " + this.h + ") | ";
        string = string + "ownerUsername=" + this.ownerUsername + " | ";
        string = string + "members=" + Arrays.toString(this.members.toArray()) + " | ";
        string = string + "membersRespawn=" + Arrays.toString(this.membersRespawn.toArray()) + " | ";
        string = string + "remove=" + this.remove + " | ";
        return string + "title=" + this.title + "] ";
    }
}
